package com.example.mq.mqserver.core;

import com.example.mq.common.ConsumerEnv;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description: MessageQueue 表示一个存储消息的队列
 * @Author: liudong
 */


public class MSGQueue {
    //使用name作为交换机的身份表示（唯一的）
    private String name;

    //该队列是否要持久化，ture需要，false不需要
    private boolean durable = false;

    //独占功能，属性为true，表示这个队列只能被一个消费者使用，别人用不了，如果为false大家都能使用
    //暂时还没实现此功能
    private boolean exclusive = false;

    //如果当前队列没人使用，就自动删除
    //暂时还没实现此功能
    private boolean autoDelete = false;

    //表示扩展参数，暂不实现
    private Map<String, Object> arguments = new HashMap<>();

    //当前队列都有哪些消费者订阅
    private List<ConsumerEnv> consumerEnvList = new ArrayList<>();

    //记录当前取到第几个消费者，方便实现轮询策略
    private AtomicInteger consumerSeq = new AtomicInteger(0);

    //添加一个新的订阅者
    public void addConsumerEnv(ConsumerEnv consumerEnv){
            consumerEnvList.add(consumerEnv);
    }
    //挑选一个订阅者，处理当前的消息
    public ConsumerEnv chooseConsumer(){
       if (consumerEnvList.size()==0){
           //没有人订阅
           return null;
       }
       //计算当前要取的元素下标
        int index = consumerSeq.get() % consumerEnvList.size();
       consumerSeq.getAndIncrement();
       return consumerEnvList.get(index);
    }
    //删除订阅者
    public  void deleteConsumerEnv(ConsumerEnv consumerEnv){
        //暂未实现
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isDurable() {
        return durable;
    }

    public void setDurable(boolean durable) {
        this.durable = durable;
    }

    public boolean isExclusive() {
        return exclusive;
    }

    public void setExclusive(boolean exclusive) {
        this.exclusive = exclusive;
    }

    public boolean isAutoDelete() {
        return autoDelete;
    }

    public void setAutoDelete(boolean autoDelete) {
        this.autoDelete = autoDelete;
    }

    public String getArguments() {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(arguments);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "{}";
    }

    public void setArguments(String argumentsJson) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            this.arguments = objectMapper.readValue(argumentsJson, new TypeReference<HashMap<String, Object>>() {
            });
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    public Object getArguments(String key) {
        return arguments.get(key);
    }

    public void setArguments(String key, Object value) {
        arguments.put(key, value);
    }

    public void setArguments(Map<String , Object> arguments){
        this.arguments = arguments;
    }
}
